package com.example.SBootDemo.Util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import com.example.SBootDemo.Util.UtilModel.Alived;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
 
public class LinuxSSH {
 
//	/**主机地址*/
//	private static String host="116.6.88.226";
//	/**连接端口*/
//	private static int port = 8020;
//	/**登入用户*/
//	private static String userName="root";
//	/**登入密码*/
//	private static String userPass="ceiecfg";
	/**执行的命令*/
	//private static String cmd = "ps -ef|grep zookeeper";
	private static String cmd = "vmstat";
	
	public static Map<String,String> cmdMap = new HashMap<String,String>() {
		{
			//项目模块
		put("elock_location","ps -ef |grep \"/root/elock/elock-location.jar\" |grep -v \"grep\"");
		put("elock_main","ps -ef |grep \"/root/elock/elock-main.jar\" |grep -v \"grep\"");
		put("elock_emq","ps -ef |grep \"/root/elock/elock-emq.jar\" |grep -v \"grep\"");
		put("elock_gateway","ps -ef |grep \"/root/elock/gateway-0.0.1-SNAPSHOT.jar\" |grep -v \"grep\"");
		put("elock_center","ps -ef |grep \"/root/elock/user-center-0.0.1-SNAPSHOT.jar\" |grep -v \"grep\"");
		put("elock_registry","ps -ef |grep \"/root/elock/registry.jar\" |grep -v \"grep\"");
		
		//三方模块
		//put("emq","ps -ef |grep \"/root/elock/registry.jar\" |grep -v \"grep\"");
		//put("redis","ps -ef |grep \"redis\" |grep -v \"grep\"");
		//put("mysql","ps -ef |grep \"mysql\" |grep -v \"grep\"");
		put("kafka","ps -ef |grep \"kafka.Kafka\" |grep -v \"grep\"");
		put("nginx","ps -ef |grep \"nginx\" |grep -v \"grep\"");
		put("zookeeper","ps -ef |grep \"/root/zookeeper-3.4.13/bin/\" |grep -v \"grep\"");
		
		}
	};
	
	
	public Alived TestAlived(String Ip,String username,String password,Map<String, String> cmdMap2 ,int Port) throws IOException {
		
		Alived alive = new Alived();
		
		Connection conn = new Connection(Ip,Port);
		conn.connect();
		//身份验证
		boolean login = conn.authenticateWithPassword(username,password);
		if (!login) {
			System.out.println("登入结果:"+login);
			return null;
		}
		System.out.println("登入成功。。。");
		//获取会画
		Session session = conn.openSession();
		//执行命令
		Alived alived = new Alived();
		
		String CmdStrin ="";
		for(String key:cmdMap2.keySet()) {
			CmdStrin+=cmdMap2.get(key)+";";
		}
		//session.execCommand(cmdMap2.get("elock_main"));
		session.execCommand(CmdStrin);
		System.out.println(CmdStrin);
		//拿到执行结果
		InputStream stdout = session.getStdout();
		InputStreamReader reader = new InputStreamReader(stdout);
		BufferedReader bufferedReader = new BufferedReader(reader);
	
		String line=null;
		while ((line=bufferedReader.readLine())!=null) {
			
            System.out.println("Result : "+line);
            
        }
		stdout.close();
		reader.close();
		bufferedReader.close();
 
		//退出工作
		
		session.getExitSignal();
		session.close();
		conn.close();
		
		return alive;
		
		
	}
	
	
	
	
	
	
	
	
	
	
//	
//	public static void main(String[] args ) throws Throwable {
//		new LinuxSSH().TestAlived("116.6.88.226", "root", "ceiecfg",cmdMap,8020);
//	}
//	
	
	
	

 
}
 
